<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>优客工厂</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:95%; margin: 0 auto">
    <div class="layui-form-item">
        <label class="layui-form-label">直播:</label>
        <div class="layui-input-inline">
            <select name="playing" lay-filter="playing" id="playing" lay-verify="required">
                <option value="">请选择直播</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">位置:</label>
        <div class="layui-input-inline" style="width: 24%">
            <select name="province" lay-filter="province" class="province" id="province">
                <option value="">请选择省</option>
            </select>
        </div>
        <div class="layui-input-inline" style="width: 24%">
            <select name="city" lay-filter="city" disabled id="city">
                <option value="">请选择市</option>
            </select>
        </div>
        <div class="layui-input-inline" style="width: 23%">
            <select name="area" lay-filter="area" disabled id="area" lay-verify="required">
                <option value="">请选择县/区</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">设备分组：</label>
        <div class="layui-input-inline">
            <select name="group" lay-filter="group" id="group" lay-verify="required"></select>
        </div>
    </div>
    <hr>
    <div class="layui-form-item layui-row layui-col-xs12">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="submit">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="../../js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script>
    layui.config({
        base: "../../js/"
    }).extend({
        "address": "address"
    })
    layui.use(['form', 'layer', "address", 'laydate', 'util'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            address = layui.address,
            laydate = layui.laydate,
            util = layui.util;

        const DELIVER_TYPE = 0; // 投放类型[0:设备分组，1:楼宇]

        var groupEl = $("#group");

        //session取值
        var areaid = sessionStorage.getItem("areaId");
        var groupid = sessionStorage.getItem("groupId");
        var playid = sessionStorage.getItem("playId");
        address.init(areaid.slice(0, 2), areaid.slice(0, 4), areaid);//初始化地址
        loadGroup(groupid);

        //渲染直播
        $.ajax({
            url: $.cookie("tempUrl") + "play/selectUnfinishedLists.do?token=" + $.cookie("token"),
            type: "POST",
            success: function (result) {
                $.each(result.data, function (index, item) {
                    if (item.id === parseInt(playid)) {
                        $("#playing").append($('<option value=' + item.id + ' selected>' + item.title + '</option>'));
                    } else {
                        $("#playing").append($('<option value=' + item.id + '>' + item.title + '</option>'));
                    }
                });
                form.render('select');
            }
        });

        //加载设备分组并初始化
        function loadGroup(groupid) {
            $.ajax({
                url: $.cookie("tempUrl") + "devicegroup/get_group.do?token=" + $.cookie("token"),
                type: "POST",
                contentType: "application/json;charset=utf-8",
                success: function (d) {
                    var groups = d.data;
                    if (groups !== null) {
                        $.each(groups, function (index, item) {
                            groupEl.append($("<option value=" + item.id + ">" + item.name + "</option>"))
                        });
                        groupEl.val(groupid);
                    }
                    form.render('select');
                }
            })
        }

        //提交
        form.on("submit(submit)", function (data) {
            //弹出loading
            var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            $.ajax({
                url: $.cookie("tempUrl") + "playdelivery/set_delivery.do?token=" + $.cookie("token"),
                type: "POST",
                datatype: "application/json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "areaid": data.field.area,
                    "delivertype": DELIVER_TYPE,
                    "groupid": data.field.group,
                    "id": $("#playing").attr("data-id"),
                    "playid": data.field.playing
                }),
                success: function (result) {
                    top.layer.close(index);
                    if (result.httpStatus == 200) {
                        top.layer.msg("修改成功！");
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    } else {
                        layer.alert(result.exception, {icon: 7, anim: 6});
                    }
                }
            });
            return false;
        })
    })
</script>
</body>
